﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Rung.EBook.Common.Driver
{
    public class StaticFunctions
    {
        public static string HtmlToTxt(string html)
        {
            if (string.IsNullOrEmpty(html)) return string.Empty;


            // 去掉【换行符】
            Regex reg = new Regex(@"[\r|\n]+", RegexOptions.IgnoreCase);
            html = reg.Replace(html, string.Empty);
            // 去掉【空格】
            reg = new Regex(@"\s+", RegexOptions.IgnoreCase);
            html = reg.Replace(html, string.Empty);
            // &nbsp;转换为【空格】
            reg = new Regex(@"&nbsp;", RegexOptions.IgnoreCase);
            html = reg.Replace(html, " ");
            // <br>转换为【换行符】
            reg = new Regex(@"<br\s*/{0,1}>", RegexOptions.IgnoreCase);
            html = reg.Replace(html, "\r\n\t");
            reg = new Regex(@"</*p>", RegexOptions.IgnoreCase);
            html = reg.Replace(html, "\r\n\t");

            return html;

            //return html.Replace("&nbsp;", "").Replace("<br>", "\r\n").Replace("<br/>", "\r\n");
        }

        public static string TxtToHtml(string text)
        {
            if (string.IsNullOrEmpty(text)) return string.Empty;

            text = text.Replace(" ", "&nbsp;").Replace("\r\n", "<br />");

            return text;
        }

        /// <summary>
        /// 获取规则文件的完整文件名。
        /// </summary>
        public static string GetRuleFileFullName()
        {
            return System.IO.Path.Combine(GetAppDataFolder(), "rules.dat");
        }

        /// <summary>
        /// 获取用户保存的信息文件夹。
        /// </summary>
        public static string GetAppDataFolder()
        {
            //return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\\Rung.eBook");
            return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
        }

        /// <summary>
        /// 获取工程文件默认的保存文件夹。
        /// </summary>
        public static string GetProjectFolderPath()
        {
            return System.IO.Path.Combine(GetAppDataFolder(), "project");
        }

        /// <summary>
        /// 获取模板文件默认的保存文件夹。
        /// </summary>
        public static string GetTemplateFolderPath()
        {
            return System.IO.Path.Combine(GetAppDataFolder(), "template");
        }

        /// <summary>
        /// 获取电子书文件默认的保存文件夹。
        /// </summary>
        public static string GetBookFolderPath()
        {
            return System.IO.Path.Combine(GetAppDataFolder(), "book");
        }

        /// <summary>
        /// 获取文本分析规则文件的完整路径。
        /// </summary>
        public static string GetTextAnalizerRulesFilePath()
        {
            return System.IO.Path.Combine(GetAppDataFolder(), "TextAnalizerRules.dat");
        }

        /// <summary>
        /// 获取文本分析规则文件的完整路径。
        /// </summary>
        public static string GeAnalizersFilePath()
        {
            return System.IO.Path.Combine(GetAppDataFolder(), "Analizeres.dat");
        }
    }
}
